#include <iostream>

// c++编译器至少给一个类添加4个函数
// 1. 构造函数
// 2. 析构函数
// 3. 拷贝构造函数
// 4. 赋值运算符重载函数 operator=,值拷贝

class Person
{
public:
    Person(int age)
    {
        m_Age = new int(age);
    }    

    ~Person()
    {
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
    }

    Person& operator=(Person &p)
    {
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
        // 深拷贝
        m_Age = new int(*p.m_Age);
        return *this;
    }

    int *m_Age;
};

void test01()
{
    Person p1(18);
    Person p2(20);
    Person p3(30);
    p3 = p2 = p1;
    std::cout << "p1 age = " << *p1.m_Age << std::endl;
    std::cout << "p2 age = " << *p2.m_Age << std::endl;
    std::cout << "p3 age = " << *p3.m_Age << std::endl;
}

int main()
{
    test01();
    return 0;
}